﻿using Stylet;
using System;
using System.Reflection;
using System.Windows.Threading;

//记录log必须的语句，整个项目只用加这一次
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace WeighingRecord.ViewModels
{
    public class ShellViewModel : Screen
    {
        //用于打开子窗口vm
        private IWindowManager windowManager;

        //启动主程序时就隐藏启动称重记录模块，在定时器中监控是否显示
        private DispatcherTimer timer = new DispatcherTimer();

        //当前显示的VM
        public object CurrentViewModel { get; set; }
        public WeightListViewModel wl;
        public WeightAccountViewModel wa;
        public WeightTotalViewModel wt;

        //此称重记录模块是否显示
        public bool IsVisible { get; set; } = false;

        private bool simpleMode;
        public bool SimpleMode
        {
            get { return simpleMode; }
            set
            {
                if (value)
                {
                    IconHeight = "0";
                    if (CurrentViewModel.GetType().Name == "WeightListViewModel")
                    {
                        ((WeightListViewModel)CurrentViewModel).SimpleMode = true;
                    }
                }
                else
                {
                    IconHeight = "*";
                    if (CurrentViewModel.GetType().Name == "WeightListViewModel")
                    {
                        ((WeightListViewModel)CurrentViewModel).SimpleMode = false;
                    }
                }
                simpleMode = value;
            }
        }

        public string IconHeight { get; set; } = "*";


        public string ProvicerTitle
        {
            get
            {
                return WeighingRecord.Globalspace.ProvicerTitle;
            }
        }


        public ShellViewModel(IWindowManager windowManager)
        {
            this.windowManager = windowManager;

            wl = new WeightListViewModel(windowManager);
            wa = new WeightAccountViewModel(windowManager);
            wt = new WeightTotalViewModel(windowManager);
           
            CurrentViewModel = wt;
           
            ResetIcon(0);
            //初始定时器
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Interval = TimeSpan.FromSeconds(0.5);
            timer.Start();

            InitializeAsposeCells();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            IsVisible = Globalspace._isVisible;
            if (Globalspace._autoUpdateQueryList && Globalspace._isVisible)
            {
                if (CurrentViewModel.GetType().Name == "WeightListViewModel")
                {
                    ((WeightListViewModel)CurrentViewModel).QueryList();
                    Globalspace._autoUpdateQueryList = false;
                }
            }
        }


        public void SwitchVM(string name)
        {
           
            switch (name)
            {
                case "WeightList":
                    CurrentViewModel = wl;
                    ResetIcon(1);
                    //var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(300) };
                    //timer.Start();
                    //timer.Tick += (sender, args) =>
                    //{
                    //    timer.Stop();
                    //    wl.SelectAll = true;
                    //};

                    break;
                case "WeightAccount":
                    CurrentViewModel = wa;
                    ResetIcon(2);
                    break;
                case "WeightTotal":
                    CurrentViewModel = wt;
                    ResetIcon(0);
                    break;
                default:
                    break;
            }
        }

        //aspose.cell破解100次限制
        internal static void InitializeAsposeCells()
        {
            const BindingFlags BINDING_FLAGS_ALL = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

            const string CLASS_LICENSER = "\u0092\u0092\u0008.\u001C";
            const string CLASS_LICENSERHELPER = "\u0011\u0001\u0006.\u001A";
            const string ENUM_ISTRIAL = "\u0092\u0092\u0008.\u001B";

            const string FIELD_LICENSER_CREATED_LICENSE = "\u0001";     // static
            const string FIELD_LICENSER_EXPIRY_DATE = "\u0002";         // instance
            const string FIELD_LICENSER_ISTRIAL = "\u0001";             // instance

            const string FIELD_LICENSERHELPER_INT128 = "\u0001";        // static
            const string FIELD_LICENSERHELPER_BOOLFALSE = "\u0001";     // static

            const int CONST_LICENSER_ISTRIAL = 1;
            const int CONST_LICENSERHELPER_INT128 = 128;
            const bool CONST_LICENSERHELPER_BOOLFALSE = false;

            //- Field setter for convinient
            Action<FieldInfo, Type, string, object, object> setValue =
                delegate (FieldInfo field, Type chkType, string chkName, object obj, object value) {
                    if ((field.FieldType == chkType) && (field.Name == chkName))
                    {
                        field.SetValue(obj, value);
                    }
                };


            //- Get types
            Assembly assembly = Assembly.GetAssembly(typeof(Aspose.Cells.License));
            Type typeLic = null, typeIsTrial = null, typeHelper = null;
            foreach (Type type in assembly.GetTypes())
            {
                if ((typeLic == null) && (type.FullName == CLASS_LICENSER))
                {
                    typeLic = type;
                }
                else if ((typeIsTrial == null) && (type.FullName == ENUM_ISTRIAL))
                {
                    typeIsTrial = type;
                }
                else if ((typeHelper == null) && (type.FullName == CLASS_LICENSERHELPER))
                {
                    typeHelper = type;
                }
            }
            if (typeLic == null || typeIsTrial == null || typeHelper == null)
            {
                //return;
                throw new Exception();
            }

            //- In class_Licenser
            object license = Activator.CreateInstance(typeLic);
            foreach (FieldInfo field in typeLic.GetFields(BINDING_FLAGS_ALL))
            {
                setValue(field, typeLic, FIELD_LICENSER_CREATED_LICENSE, null, license);
                setValue(field, typeof(DateTime), FIELD_LICENSER_EXPIRY_DATE, license, DateTime.MaxValue);
                setValue(field, typeIsTrial, FIELD_LICENSER_ISTRIAL, license, CONST_LICENSER_ISTRIAL);
            }

            //- In class_LicenserHelper
            foreach (FieldInfo field in typeHelper.GetFields(BINDING_FLAGS_ALL))
            {
                setValue(field, typeof(int), FIELD_LICENSERHELPER_INT128, null, CONST_LICENSERHELPER_INT128);
                setValue(field, typeof(bool), FIELD_LICENSERHELPER_BOOLFALSE, null, CONST_LICENSERHELPER_BOOLFALSE);
            }
        }


        /// <summary>
        /// 称重统计图片
        /// </summary>
        public string cztjImg { get; set; }
        /// <summary>
        /// 称重记录图片
        /// </summary>
        public string czjlImg { get; set; }
        /// <summary>
        /// 称重查询图片
        /// </summary>
        public string czcxImg { get; set; }

        public string xztjForeground { get; set; }

        public string czjlForeground { get; set; }

        public string czcxForeground { get; set; }

        public void ResetIcon(int selectIndex = 0)
        {
            cztjImg = string.Format("/Resources/Img/07/bda{0}.png", selectIndex == 0 ? "a" : "");
            czjlImg = string.Format("/Resources/Img/07/bdb{0}.png", selectIndex == 1 ? "b" : "");
            czcxImg = string.Format("/Resources/Img/07/bdc{0}.png", selectIndex == 2 ? "c" : "");

            xztjForeground = selectIndex == 0 ? "#000000" : "#666666";
            czjlForeground = selectIndex == 1 ? "#000000" : "#666666";
            czcxForeground = selectIndex == 2 ? "#000000" : "#666666";

        }

    }
}
